package com.sy.simpleegg.ui.banner.adapter

import android.util.Log
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.sy.simpleegg.R
import com.sy.simpleegg.ui.banner.bean.ResourceBean
import com.sy.simpleegg.ui.banner.holder.ImageHolder
import com.sy.simpleegg.ui.banner.holder.VideoHolder
import com.youth.banner.util.BannerUtils

/**
 * @param mDatas 广告资源
 * @param defaultImageRes 默认图片
 * @param isTouchWidget 滑动设置视频进度
 */
class MediaVideoBannerAdapter(mDatas: MutableList<ResourceBean>, private val defaultImageRes: Int, private val isTouchWidget: Boolean) :
    BaseBannerAdapter<ResourceBean, RecyclerView.ViewHolder>(mDatas) {
    private val videoHolderMap: MutableMap<Int, VideoHolder>

    interface OnBannerVideoPlayListener {
        fun onVideoPlayStart()
        fun onVideoPlayComplete()
    }

    init {
        videoHolderMap = HashMap()
    }
    
    override fun onCreateHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
        when (viewType) {
            1 -> return ImageHolder(BannerUtils.getView(parent!!, R.layout.banner_image))
            2 -> return VideoHolder(BannerUtils.getView(parent!!, R.layout.banner_video))
        }
        return ImageHolder(BannerUtils.getView(parent!!, R.layout.banner_image))
    }

    override fun getItemViewType(position: Int): Int {
        return getData(getRealPosition(position)).type
    }

    override fun onBindView(
        holder: RecyclerView.ViewHolder,
        data: ResourceBean,
        position: Int,
        size: Int,
        adapterPosition: Int
    ) {
        val viewType = holder.itemViewType
        when (viewType) {
            1 -> {
                val imageHolder = holder as ImageHolder
                Glide.with(imageHolder.itemView)
                    .load(if (data.res == -1) data.url else data.res)
                    .placeholder(defaultImageRes)
                    .into(imageHolder.imageView)
            }

            2 -> {
                val videoHolder = holder as VideoHolder
                videoHolder.player.setIsTouchWiget(isTouchWidget)
                videoHolder.player.isStartAfterPrepared = true
                videoHolder.player.isNeedLockFull = true
                videoHolder.player.setUp(data.url, true, null)
                videoHolder.player.backButton.visibility = View.GONE
                //增加封面
                /*ImageView imageView = new ImageView(context);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                //imageView.setImageResource(R.drawable.image4);
                videoHolder.player.setThumbImageView(imageView);*/videoHolderMap[adapterPosition] =
                    videoHolder
            }
        }
    }

    fun getVideoHolder(position: Int): VideoHolder? {
        return videoHolderMap[position]
    }

    fun stopVideo() {
        for (key in videoHolderMap.keys) {
            try {
                if (getVideoHolder(key) != null) {
                    getVideoHolder(key)!!.player.release()
                    getVideoHolder(key)!!.player.onVideoReset()
                }
            } catch (e: Throwable) {
                Log.e("VideoBannerError", "停止视频播放出错 " + e.localizedMessage)
            }
        }
    }
}
